package com.hh.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.constant.QueueConstant;
import com.hh.entity.SmsLog;
import com.hh.entity.User;
import com.hh.mapper.UserMapper2;
import com.hh.model.AliSmsModel;
import com.hh.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 辉辉
 * @since 2022-08-19
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper2, User> implements UserService {

    @Resource
    private UserMapper2 userMapper2;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 更新用户
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(User entity) {
        entity.setModifyTime(new Date());
        entity.setUserLasttime(new Date());
        int i = userMapper2.updateById(entity);
        return i > 0;
    }

    /**
     * 发送短信验证码
     * 1. 自定义生成一个code
     * 2. 放到redis里
     * 3. 设置过期时间 5分钟
     * 4. 组装参数
     * 5。 放入mq
     * 6. 返回出去
     *
     * @param openId
     * @param sendMap
     */
    @Override
    public void sendMsg(String openId, Map<String, String> sendMap) {
        String phoneNumber = sendMap.get("phonenum");
        // 生成验证码
        String code = createCode();
        // 放入redis
        redisTemplate.opsForValue().set(phoneNumber, code, Duration.ofMinutes(5));
        // 组装参数 短信的签名 短信的模板 短信的code
        AliSmsModel aliSmsModel = new AliSmsModel();
        aliSmsModel.setPhoneNumbers(phoneNumber);
        aliSmsModel.setSigName("ego商城");
        aliSmsModel.setTemplateCode("SMS_203185255");
        Map<String, String> map = new HashMap<>(2);
        map.put("Code", code);
        aliSmsModel.setTemplateParam(JSON.toJSONString(map));
        // 放mq
        rabbitTemplate.convertAndSend(QueueConstant.PHONE_SEND_EX, QueueConstant.PHONE_SEND_KEY, JSON.toJSONString(aliSmsModel));
    }

    private String createCode() {
        // 生成验证码
        // 定 位数 8搞多一点
        // 随机数生成验证码
        return "888888";
    }


    /**
     * 保存手机号
     * 1. 判断redis是否有key
     *
     * @param openId
     * @param sendMap
     */
    @Override
    public void savePhone(String openId, Map<String, String> sendMap) {
        String phonenum = sendMap.get("phonenum");
        String redisCode = redisTemplate.opsForValue().get(phonenum);
        // 判断前台传进来的和redis是否一致
        String code = sendMap.get("code");
        if (!StringUtils.isEmpty(redisCode) && code.equals(redisCode)) {
            // 说明是对的 记录数据库
            User user = new User();
            user.setUserMobile(phonenum);
            userMapper2.update(user, Wrappers.<User>lambdaUpdate()
                    .eq(User::getUserId, openId)
            );
            // redis干掉key
            redisTemplate.delete(phonenum);
        }
    }
}
